package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.domain.BProject;
import com.ruoyi.system.domain.request.ProjectRequest;
import com.ruoyi.system.domain.vo.*;
import com.ruoyi.system.mapper.BProjectTaskMapper;
import com.ruoyi.system.service.BProjectCompanyService;
import com.ruoyi.system.service.BProjectService;
import com.ruoyi.system.mapper.BProjectMapper;
import com.ruoyi.system.service.BProjectUserService;
import com.ruoyi.system.service.BScoreRuleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author yangxingze
* @description 针对表【b_project(项目表)】的数据库操作Service实现
* @createDate 2024-12-28 00:45:04
*/
@Service
public class BProjectServiceImpl extends ServiceImpl<BProjectMapper, BProject>
    implements BProjectService{

    @Resource
    BProjectTaskMapper bProjectTaskMapper;

    @Autowired
    BProjectCompanyService projectCompanyService;

    @Autowired
    BProjectUserService projectUserService;

    @Autowired
    BScoreRuleService scoreRuleService;

    /**
     * 统计任务数
     * @param userId
     * @return
     */
    @Override
    public TaskDataVo getData(Long userId) {
        Integer totalNum = bProjectTaskMapper.getTaskTotalNum(userId);
        Integer incompleteNum = bProjectTaskMapper.getTaskIncompleteNum(userId);
        TaskDataVo taskDataVo = new TaskDataVo();
        taskDataVo.setTotalNum(totalNum);
        taskDataVo.setIncompleteNum(incompleteNum);
        if(totalNum > 0){
            taskDataVo.setDiffRate(String.format("%.2f", (totalNum-incompleteNum) * 100.0 / totalNum));
        }
        return taskDataVo;
    }

    /**
     * 根据药企获取用户列表
     * @param companyIds
     * @return
     */
    @Override
    public List<BUserVo> selectUserListByCompanyIds(Long[] companyIds) {
        return baseMapper.selectUserListByCompanyIds(companyIds);
    }

    /**
     * 分页获取所属任务的项目列表
     * @param request
     * @return
     */
    @Override
    public List<BProjectVo> queryPage(ProjectRequest request) {
        List<BProjectVo> list = baseMapper.queryPage(request);
        return list;
    }

    /**
     * 新增项目
     * @param projectVo
     * @return
     */
    @Override
    public boolean saveVo(BProjectVo projectVo) {
        List<BCompanyVo> companyList = projectVo.getCompanyList();
        List<BScoreRuleVo> scoreRuleList = projectVo.getScoreRuleList();
        List<BUserVo> userList = projectVo.getUserList();
        BProject project = new BProject();
        BeanUtils.copyProperties(projectVo, project);
        project.setCreateTime(new Date());
        boolean result = this.save(project);
        if(result){
            //新增项目公司关联
            projectCompanyService.saveProjectCompany(companyList, project.getId());
            //新增评分标准
            scoreRuleService.saveScoreRule(scoreRuleList, project.getId());
            //新增用户关联
            projectUserService.saveProjectUser(userList, project.getId());
        }
        return result;
    }

    /**
     * 更新项目
     * @param projectVo
     * @return
     */
    @Override
    public boolean updateVo(BProjectVo projectVo) {
        List<BCompanyVo> companyList = projectVo.getCompanyList();
        List<BScoreRuleVo> scoreRuleList = projectVo.getScoreRuleList();
        List<BUserVo> userList = projectVo.getUserList();
        BProject project = new BProject();
        BeanUtils.copyProperties(projectVo, project);
        project.setUpdateTime(new Date());
        boolean result = this.updateById(project);
        if(result){
            //删除原有数据
            this.deletesByProjectId(project.getId());

            //新增项目公司关联
            projectCompanyService.saveProjectCompany(companyList, project.getId());
            //新增评分标准
            scoreRuleService.saveScoreRule(scoreRuleList, project.getId());
            //新增用户关联
            projectUserService.saveProjectUser(userList, project.getId());
        }
        return result;
    }

    /**
     * 获取项目列表
     * @param project
     * @return
     */
    @Override
    public List<BProjectVo> selectProjectList(BProject project) {
        return baseMapper.selectProjectList(project);
    }

    /**
     * 获取项目详情
     * @param id
     * @return
     */
    @Override
    public BProjectVo selectProjectById(Long id) {
        return baseMapper.selectProjectById(id);
    }

    /**
     * 删除项目
     * @param ids
     * @return
     */
    @Override
    public int deleteProjectByIds(Long[] ids) {
        int result = baseMapper.deleteProjectByIds(ids);
        if(result > 0){
            for(Long projectId: ids){
                deletesByProjectId(projectId);
            }
        }
        return result;
    }

    public void deletesByProjectId(Long projectId){
        //删除药企关联
        projectCompanyService.deleteProjectCompanyByProjectId(projectId);
        //删除用户关联
        projectUserService.deleteProjectUserByProjectId(projectId);
        //删除评分标准
        scoreRuleService.deleteScoreRuleByProjectId(projectId);
    }
}




